<?php
	class ModelNewsNews extends Model
	{
		public function addNews($data) 
		{
			echo '<pre>';
			print_r($data);
			echo '</pre>';
			
			$this->db->query("INSERT INTO " . DB_PREFIX . "news SET news_category_id = '" . (int)$data['parent_id'] . "', sort_order = '" . (int)$data['sort_order'] . "', created_date = NOW(), updated_date = NOW(), status = '" . (int)$data['status'] . "'" );
	
			$news_id = $this->db->getLastId();
			
			foreach ($data['news_description'] as $language_id => $value) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "news_description SET news_id='" . $news_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', short_description = '" . $this->db->escape($value['short_description']) . "', description = '" . $this->db->escape($value['description']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', author = '" . $this->db->escape($value['author']) . "', image = '" . $this->db->escape($data['image']) . "'");			}
			
						
			if ($data['keyword']) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'news_id=" . (int)$news_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
			}
			
			$this->cache->delete('news');

		}
		
		
		public function editNews($news_id, $data) 
		{
			
			$this->db->query("UPDATE " . DB_PREFIX . "news SET news_category_id = '" . (int)$data['parent_id'] . "', sort_order = '" . (int)$data['sort_order'] . "', created_date = NOW(), updated_date = NOW(), status = '" . (int)$data['status'] . "' WHERE news_id = '" . (int)$news_id . "'");

			$this->db->query("DELETE FROM " . DB_PREFIX . "news_description WHERE news_id = '" . (int)$news_id . "'");

			foreach ($data['news_description'] as $language_id => $value) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "news_description SET news_id='" . (int)$news_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', short_description = '" . $this->db->escape($value['short_description']) . "', description = '" . $this->db->escape($value['description']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', author = '" . $this->db->escape($value['author']) . "', image = '" . $this->db->escape($data['image']) . "'");	
			}
							
			$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'news_id=" . (int)$news_id. "'");
			
			if ($data['keyword']) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'news_id=" . (int)$news_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
			}
			
			$this->cache->delete('news');
		}
		
		public function deleteNews($news_id) 
		{
			$this->db->query("DELETE FROM " . DB_PREFIX . "news WHERE news_id = '" . (int)$news_id . "'");
			$this->db->query("DELETE FROM " . DB_PREFIX . "news_description WHERE news_id = '" . (int)$news_id . "'");
			//$this->db->query("DELETE FROM " . DB_PREFIX . "product_to_category WHERE category_id = '" . (int)$category_id . "'");
			$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'news_id=" . (int)$news_id . "'");
		
			$this->cache->delete('news');
		}
		
		public function getNew($news_id) //lay ra tu khoa khi search
		{
			$query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'news_id=" . (int)$news_id . "') AS keyword FROM " . DB_PREFIX . "news_description WHERE news_id = '" . (int)$news_id . "'");

			return $query->row;
		} 
		
		public function getPath($news_id) 
		{
			$query = $this->db->query("SELECT title FROM " . DB_PREFIX . "news_description c LEFT JOIN " . DB_PREFIX . "news cd ON (c.news_id = cd.news_id) WHERE c.news_id = '" . $news_id . "' AND c.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY cd.sort_order, c.title ASC");
		
			return $query->row['title'];
		}
		
		public function getAuthor($news_id) 
		{
			$query = $this->db->query("SELECT author FROM " . DB_PREFIX . "news_description c LEFT JOIN " . DB_PREFIX . "news cd ON (c.news_id = cd.news_id) WHERE c.news_id = '" . $news_id . "' AND c.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY cd.sort_order, c.author ASC");
		
			return $query->row['author'];
		}
		
		public function getImage($news_id) 
		{
			$query = $this->db->query("SELECT image FROM " . DB_PREFIX . "news_description c LEFT JOIN " . DB_PREFIX . "news cd ON (c.news_id = cd.news_id) WHERE c.news_id = '" . $news_id . "' AND c.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY cd.sort_order, c.image ASC");
			return $query->row['image'];
		}
		
		public function getSortOrder($news_id) 
		{
			$query = $this->db->query("SELECT sort_order FROM " . DB_PREFIX . "news_description c LEFT JOIN " . DB_PREFIX . "news cd ON (c.news_id = cd.news_id) WHERE c.news_id = '" . $news_id . "' AND c.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY cd.sort_order ASC");
			return $query->row['sort_order'];
		}
		
		public function getStatus($news_id) 
		{
			$query = $this->db->query("SELECT status FROM " . DB_PREFIX . "news_description c LEFT JOIN " . DB_PREFIX . "news cd ON (c.news_id = cd.news_id) WHERE c.news_id = '" . $news_id . "' AND c.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY cd.sort_order, cd.status ASC");
			return $query->row['status'];
		}
		
		public function getDanhmuc($news_id)
		{
			$query = $this->db->query("SELECT news_name FROM " . DB_PREFIX . "news c LEFT JOIN " . DB_PREFIX . "news_category_description cd ON (c.news_category_id=cd.news_category_id) WHERE c.news_id ='" . $news_id . "' AND cd.language_id='" . (int)$this->config->get('config_language_id') . "' ORDER BY c.sort_order, cd.news_name ASC");
			return $query->row['news_name'];
		}
		
		public function getNewsCategoryId($news_id)
		{
			$query = $this->db->query("SELECT news_category_id FROM " . DB_PREFIX . "news_description c LEFT JOIN " . DB_PREFIX . "news cd ON (c.news_id = cd.news_id) WHERE c.news_id = '" . $news_id . "' AND c.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY cd.sort_order, cd.news_category_id ASC");
			return $query->row['news_category_id'];
		}
		
		public function getMangDanhmuc()
		{
			$news_data = array();
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_category_description GROUP BY news_category_id, news_name");
			foreach ($query->rows as $result)
			{
				$news_data[]=array(
					'news_category_id'		=> $result['news_category_id'],
					'news_name'				=> $result['news_name']
				);
			}
			return $news_data;
		}
		
		public function getNewsDescription($parent_id = 0) {
			$news_data = array();
		
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_description c LEFT JOIN " . DB_PREFIX . "news cd ON (c.news_id = cd.news_id) WHERE c.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY c.title, c.author, cd.sort_order ASC");

			foreach ($query->rows as $result) {
				$news_data[] = array(
					'news_id' 		=> $result['news_id'],
					'news_category_id'	=> $result['news_category_id'],
					'title'  		 => $this->getPath($result['news_id'], $this->config->get('config_language_id')),
					'author'		=> $this->getAuthor($result['news_id'],$this->config->get('config_language_id')),
					'news_name'		=> $this->getDanhmuc($result['news_id'],$this->config->get('config_language_id')),	
					'image'			=> $this->getImage($result['news_id'],$this->config->get('config_language_id')),
					'status'  	  => $result['status'],
					'sort_order'  => $result['sort_order']
				);

				//$news_data = array_merge($news_data, $this->getNewsDescription($result['news_id']));
			}	
	
		return $news_data;
	}
	
	public function getNew1($news_id) 
	{
		$news_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_description WHERE news_id = '" . (int)$news_id . "'");
		
		foreach ($query->rows as $result) {
			$news_data[$result['language_id']] = array(
				'news_id'		   => $result['news_id'],
				'news_name'		   => $this->getDanhmuc($result['news_id'],$this->config->get('config_language_id')),
				'news_category_id' => $this->getNewsCategoryId($result['news_id'],$this->config->get('config_language_id')),
				'title'        	   => $result['title'],
				'short_description' => $result['short_description'],
				'meta_keyword'     => $result['meta_keyword'],
				'meta_description' => $result['meta_description'],
				'description'      => $result['description'],
				'author'		   => $result['author'],
				'image'			   => $result['image']
			);
		}
		
		return $news_data;
	}	
		
		public function getNews($news_id) 
		{
			$news_data = array();
			
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news WHERE news_id = '" . (int)$new_id . "'");
			
			foreach ($query->rows as $result) {
				$news_data[$result['language_id']] = array(
					'news_name'        => $result['news_name'],
					'meta_keyword'     => $result['meta_keyword'],
					'meta_description' => $result['meta_description'],
					'description'      => $result['description']
				);
			}
			
			return $news_data;
		}	
		
		public function getNewsStores($news_id) 
		{
			$news_store_data = array();
			
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_to_store WHERE news_id = '" . (int)$news_id . "'");

			foreach ($query->rows as $result) {
				$news_store_data[] = $result['store_id'];
			}
			
			return $news_store_data;
		}
		
		public function getNewsLayouts($news_id) 
		{
			$news_layout_data = array();
			
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_to_layout WHERE news_id = '" . $this->db->escape($value['news_id']) . "'");
			
			foreach ($query->rows as $result) {
				$news_layout_data[$result['store_id']] = $result['layout_id'];
			}
			
			return $news_layout_data;
		}
	}
?>